<template>
    <div class="Analysis-content">
        <div class="title">社区入住分析</div>
        <div class="content-gap ">
            <div class="content-gap-item ">
                <div class="content-gap-item-info ">
                    <div class="content-gap-item-title">自住房屋</div>
                    <div class="content-gap-item-count">
                        <span class="count-number" data-value="900876">0</span>
                    </div>
                </div>
                <div class="content-gap-item-bg housing"> </div>
            </div>
            <div class="content-gap-item">
                <div class="content-gap-item-info">
                    <div class="content-gap-item-title">出租房屋</div>
                    <div class="content-gap-item-count">
                        <span class="count-number" data-value="456789">0</span>
                    </div>
                </div>
                <div class="content-gap-item-bg rent"> </div>
            </div>
            <div class="content-gap-item">
                <div class="content-gap-item-info">
                    <div class="content-gap-item-title">空置房屋</div>
                    <div class="content-gap-item-count">
                        <span class="count-number" data-value="123456">0</span>
                    </div>
                </div>
                <div class="content-gap-item-bg Availability"> </div>
            </div>
        </div>
        <!-- 进度条 -->
        <div class="progress-bar">
            <CanvasProgressBar :progress-data="[
                { percentage: 70, label: '入住率' }
            ]" :gradient-colors="['#3581F2', '#22B2F0 ']" :background-color="'#f5f5f5'" />
        </div>
    </div>
</template>

<script setup>

import { onMounted, nextTick } from 'vue'
import CanvasProgressBar from '@/components/CanvasProgressBar.vue'

// 数字滚动动画函数
const animateNumber = (element, targetValue, duration = 800) => {
    const startValue = 0
    const startTime = performance.now()

    const updateNumber = (currentTime) => {
        const elapsed = currentTime - startTime
        const progress = Math.min(elapsed / duration, 1)

        // 使用缓动函数让动画更自然
        const easeOutQuart = 1 - Math.pow(1 - progress, 4)
        const currentValue = Math.floor(startValue + (targetValue - startValue) * easeOutQuart)

        // 格式化数字，添加千分位分隔符
        element.textContent = currentValue.toLocaleString()

        if (progress < 1) {
            requestAnimationFrame(updateNumber)
        }
    }

    requestAnimationFrame(updateNumber)
}

// 观察器回调函数
const observerCallback = (entries) => {
    entries.forEach(entry => {
        if (entry.isIntersecting) {
            const countElement = entry.target.querySelector('.count-number')
            if (countElement) {
                const targetValue = parseInt(countElement.dataset.value)
                animateNumber(countElement, targetValue)
            }
        }
    })
}

onMounted(async () => {
    await nextTick()
    // 创建交叉观察器
    const observer = new IntersectionObserver(observerCallback, {
        threshold: 0.5,
        rootMargin: '0px 0px -50px 0px'
    })

    // 观察所有数字元素
    const countElements = document.querySelectorAll('.content-gap-item')
    countElements.forEach(item => observer.observe(item))
})
</script>
<style scoped lang="scss">
.Analysis-content {
    width: 100%;
    height: 280px;

    padding: 30px 0px 18px 26px;

    .content-gap {
        width: 100%;
        height: 81%;
        display: flex;
        margin-top: 22px;
        margin-left: -10px;
        justify-content: space-between;

        .content-gap-item {
            width: 30%;
            height: 150px;
            opacity: 0;
            transform: translateY(30px);
            animation: fadeInUp 0.8s ease forwards;

            &:nth-child(1) {
                animation-delay: 0.1s;
            }

            &:nth-child(2) {
                animation-delay: 0.3s;
            }

            &:nth-child(3) {
                animation-delay: 0.5s;
            }

            .content-gap-item-bg {
                width: 100%;
                height: 80%;
                background-size: 100% 100%;
                background-repeat: no-repeat;
                background-position: center;

                .content-gap-item-icon {
                    width: 40px;
                    height: 40px;
                    border-radius: 50%;
                    background-color: #00B85C;
                    margin-top: 12px;
                    display: flex;
                    align-items: center;
                    justify-content: center;
                }

                .content-gap-item-icon img {
                    width: 24px;
                    height: 24px;
                    filter: brightness(0) invert(1);
                    /* 将图标改为白色 */
                }

                .content-gap-item-icon img {
                    width: 80%;
                    height: 80%;
                }
            }

            .content-gap-item-info {
                width: 100%;
                height: 50px;
                display: flex;
                align-items: center;
                justify-content: center;
                flex-direction: column;
                gap: 10px;

                .content-gap-item-title {
                    font-family: 微软雅黑;
                    font-weight: 500;
                    font-size: 15px;
                    color: #15365B;
                    opacity: 0.8;
                    position: relative;
                    transition: all 0.3s ease;

                    &::after {
                        content: '';
                        position: absolute;
                        bottom: -2px;
                        left: 50%;
                        width: 0;
                        height: 2px;
                        background: linear-gradient(90deg, #00B85C, #00D4AA);
                        transition: all 0.3s ease;
                        transform: translateX(-50%);
                    }

                    &:hover::after {
                        width: 100%;
                    }
                }

                .content-gap-item-count {
                    font-weight: 600;
                    font-size: 20px;
                    color: #00B85C;
                    position: relative;

                    .count-number {
                        display: inline-block;
                        position: relative;
                        font-family: 'Courier New', monospace;
                        font-size: 20px;
                        font-weight: 700;
                        background: linear-gradient(135deg, #00B85C 0%, #00D4AA 100%);
                        -webkit-background-clip: text;
                        -webkit-text-fill-color: transparent;
                        background-clip: text;
                        text-shadow: 0 2px 4px rgba(0, 184, 92, 0.3);
                        transition: all 0.3s ease;

                        &::before {
                            content: '';
                            position: absolute;
                            top: -2px;
                            left: -2px;
                            right: -2px;
                            bottom: -2px;
                            background: linear-gradient(135deg, #00B85C, #00D4AA);
                            border-radius: 4px;
                            z-index: -1;
                            opacity: 0;
                            transition: opacity 0.3s ease;
                        }

                        &:hover::before {
                            opacity: 0.1;
                        }
                    }
                }
            }
        }

        .progress-bar {
            width: 100%;
        }
    }

    .title {
        color: #141414;
        font-family: Alimama FangYuanTi VF;
        font-weight: normal;
        font-size: 16px;
        font-family: PingFang SC;
        font-weight: bold;
        font-size: 20px;
        color: #383C42;
        // 左边蓝色的线
        // 设置间距
        position: relative;

        &::before {
            content: '';
            position: absolute;
            left: 0;
            top: 50%;
            transform: translateY(-50%);
            width: 4px;
            height: 100%;
            background-color: #1872F9;
            border-radius: 10px;
            margin-left: -10px;
            margin-top: 1px;
        }

    }

    // 入场动画关键帧
    @keyframes fadeInUp {
        from {
            opacity: 0;
            transform: translateY(30px);
        }

        to {
            opacity: 1;
            transform: translateY(0);
        }
    }
}

.housing {
    background-image: url('@/assets/image/screen/Compotes/file_qx261.png');
}

.rent {
    background-image: url('@/assets/image/screen/Compotes/file_71zmh.png');
}

.Availability {
    background-image: url('@/assets/image/screen/Compotes/file_wl3ab.png');
}
</style>